package ml.uuku.oschub.ui;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.OnClick;
import ml.uuku.oschub.AppContext;
import ml.uuku.oschub.R;
import ml.uuku.oschub.base.BaseFragment;

/**
 * @author <a href=http://my.oschina.net/uuku>uuku</a>
 * @version V0.1
 */
public class NavigationDrawableFragment extends BaseFragment {

    private static final String STATE_SELECTED_POSITION = "selected_navigation_drawer_position";

    private NavigationDrawerCallback mCallback;

    private ActionBarDrawerToggle mDrawerToggle;
    private DrawerLayout mDrawerLayout;
    private View mDrawerListView;
    private View mFragmentContentView;

    private int mCurrentSelectedPosition = 0;
    private boolean mFromSavedInstanceState;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (savedInstanceState != null) {
            mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION);
            mFromSavedInstanceState = true;
        }
        setSelectItem(mCurrentSelectedPosition);
    }

    private void setSelectItem(int position) {
        mCurrentSelectedPosition = position;
        if (mDrawerLayout != null) {
            mDrawerLayout.closeDrawer(mFragmentContentView);
        }
        if (mCallback != null) {
            mCallback.onNavigationDrawerItemSelected(position);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        mDrawerListView = inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
        ButterKnife.bind(this, mDrawerListView);
        initView(mDrawerListView);
        initData();

        return mDrawerListView;
    }

    @Override
    public void initView(View view) {
        TextView night = (TextView) view.findViewById(R.id.tv_night);
        if (AppContext.getNightModeSwitch()) {
            night.setText("日间");
        } else {
            night.setText("夜间");
        }

    }

    @Override
    public void initData() {

    }

    /**
     * 调用者必须使用该方法来设置导航菜单
     * 
     * @param fragmentId 该Fragment在它的Activity布局中的id
     * @param drawerLayout 容纳该Fragment的DrawerLayout对象
     */
    public void setup(int fragmentId, DrawerLayout drawerLayout) {
        mFragmentContentView = getActivity().findViewById(fragmentId);
        mDrawerLayout = drawerLayout;
        // 在侧边栏菜单打开时，在主内容上设置一个阴影
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, Gravity.LEFT);

        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeButtonEnabled(true);

        mDrawerToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout, null,
                R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                getActivity().invalidateOptionsMenu();
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                getActivity().invalidateOptionsMenu();
            }
        };

        mDrawerLayout.post(new Runnable() {
            @Override
            public void run() {
                mDrawerToggle.syncState();
            }
        });

        mDrawerLayout.setDrawerListener(mDrawerToggle);
    }

    public interface NavigationDrawerCallback {
        void onNavigationDrawerItemSelected(int position);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mCallback = (NavigationDrawerCallback) activity;
        } catch (ClassCastException cce) {
            throw new ClassCastException("Activity must implement NavigationDrawerCallback");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mCallback = null;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(STATE_SELECTED_POSITION, mCurrentSelectedPosition);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @OnClick({
            R.id.menu_item_quests, R.id.menu_item_opensoft, R.id.menu_item_blog, R.id.menu_item_gitapp
            , R.id.menu_item_setting, R.id.menu_item_theme
            , R.id.ll_navi_drawer
            /* 点击菜单栏其他区域，则关闭菜单栏 */
    })
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.menu_item_quests:
                // 技术问答
                break;
            case R.id.menu_item_opensoft:
                // 开源软件
                break;
            case R.id.menu_item_blog:
                // 博客区
                break;
            case R.id.menu_item_gitapp:
                // Git 客户端
                break;
            case R.id.menu_item_setting:
                // 设置
                break;
            case R.id.menu_item_theme:
                // 夜间
                break;
        }

        mDrawerLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                mDrawerLayout.closeDrawers();
            }
        }, 2000);
    }

    private ActionBar getActionBar() {
        return ((ActionBarActivity) getActivity()).getSupportActionBar();
    }
}
